package reflect;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URISyntaxException;

public class Test4 {
    public static void main(String[] args) throws URISyntaxException, ClassNotFoundException, InstantiationException, IllegalAccessException, InvocationTargetException {
        File dir = new File(Test4.class.getResource(".").toURI());
        File[] subs = dir.listFiles(f->f.getName().endsWith(".class"));
        for (File sub : subs){
            String fileName = sub.getName();
            String className = fileName.substring(0,fileName.indexOf("."));
            Class cls = Class.forName(Test4.class.getPackage().getName()+"."+className);
            if (cls.isAnnotationPresent(AutoRunClass.class)){
                System.out.println("正在实例化:"+cls.getName());
                Object obj = cls.newInstance();
                System.out.println(obj);

                Method[] methods = cls.getDeclaredMethods();
                for (Method method : methods){
                    if (method.isAnnotationPresent(AutoRunMethod.class)){
                        System.out.println("自动调用:"+method.getName()+"()");
                        method.invoke(obj);
                    }
                }
            }
        }
    }
}
